<?php
/**
 * Cliente para acesso ao webservice das anedotas
 **/
 
$url = "http://62.28.132.141/phprestsql/TipoAnedota";
$tiposAnedotas = array();
$anedotas = array();

getTipoAnedotas($url);
$url = "http://62.28.132.141/phprestsql/Anedota";
getAnedotas($url);
 
function getTipoAnedotas($url2){
	$xml = file_get_contents($url2);
 	if($xml == null) return;
	$sxml = simplexml_load_string($xml);
      
	foreach ($sxml->row as $rows){
	    getNosTipoAnedotas($url2 . "/" . $rows);
	}
}
 
 
function getNosTipoAnedotas($url3){
    global $tiposAnedotas;
    $xml = file_get_contents($url3);
    $sxml = simplexml_load_string($xml);
    $tiposAnedotas[(int)$sxml->idTipoAnedota] = (string)$sxml->Descricao;
}
		 
		 
function getAnedotas($url2){
    $xml = file_get_contents($url2);
    if($xml == null) return;
    $sxml = simplexml_load_string($xml);
		    
    foreach ($sxml->row as $rows){
       getNosAnedotas($url2 . "/" . $rows);
    }
		    
}
		
function getNosAnedotas($url3){
	global $anedotas;
	$xml = file_get_contents($url3);
	$xml = utf8_encode($xml); //derivado aos caracteres xml portugueses no xml, evita erros do simpleXML
	$sxml = simplexml_load_string($xml);
	 
	$anedotas[(int)$sxml->idAnedota] = array((int)$sxml->IdTipoAnedota => (string)$sxml->Texto);
}

function mostraAnedotas($tipo,$doDia=0){
	global $tiposAnedotas;
	global $anedotas;
	
	$stringSplit = explode("-",$tipo);
	 
	$tipoAned = trim($stringSplit[0]);
	$tipoDesc = trim($stringSplit[1]);
	
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";
	echo "<h1> Anedotas de " . $tipoDesc . "</h1>";
	echo "<form name=\"editaElimina\" action=\"" . htmlentities($_SERVER['PHP_SELF']) . "\" method=\"GET\" >";
	echo "<input type=\"hidden\" name=\"TipoAnedota\" value=\"" . $tipoAned . "\">";
	echo "<a href=\"http://localhost/cliente.php\">Home</a>";
	echo "<input type=\"submit\" name=\"Operacao\" value=\"Nova\" />";
	
	if ($doDia == 1){
		$arrtmp = array();
		//percorro o array coloco as chaves das anedotas do tipo no array e escolho uma aleatoria
		foreach($anedotas as $key => $value) {
			foreach($value as $key2 => $aned){
				if($key2 == $tipo){
					$arrtmp[]=$key;
				}
			}
		}
		//obter o indice da anedota
		$aleat = rand(0,count($arrtmp) - 1);
		$chaveAned = $arrtmp[$aleat];
		foreach($anedotas as $key => $value) {
			foreach($value as $key2 => $aned){
				if($key == $chaveAned){
					echo "<div id=\"anedota\">";
 						echo "<h2>Anedota: " . $key . "</h2>";
 						echo "<p>" . $aned . "</p>";
 						echo "<input type=\"hidden\" name=\"Anedota\" value=\"" . $key . "\">";
 						echo "<input type=\"hidden\" name=\"TextoAnedota\" value=\"" . $aned . "\">";
 						echo "<input type=\"submit\" name=\"Operacao\" value=\"Eliminar\" />";
 						echo "<input type=\"submit\" name=\"Operacao\" value=\"Editar\" />";
 						echo "</form></p>";   
 					echo "</div>";
					break;
				}
			}
		}
						
	}else{
		foreach($anedotas as $key => $value) {
			foreach($value as $key2 => $aned){
 				if($key2 == $tipo){
 					echo "<div id=\"anedota\">";
 						echo "<h2>Anedota: " . $key . "</h2>";
 						echo "<p>" . $aned . "</p>";
 						echo "<p><form name=\"editaElimina\" action=\"" . htmlentities($_SERVER['PHP_SELF']) . "\" method=\"GET\" >";
 						echo "<input type=\"hidden\" name=\"Anedota\" id=\"utilizador\" value=\"" . $key . "\">";
 						echo "<input type=\"hidden\" name=\"TextoAnedota\" value=\"" . $aned . "\">";
 						echo "<input type=\"submit\" name=\"Operacao\" value=\"Eliminar\" size=\"200\"/>";
 						echo "<input type=\"submit\" name=\"Operacao\" value=\"Editar\" />";
 						echo "</form></p>";   
 					echo "</div>";
  				} 					
 			}
 		} 
	}
}

function efectuaOperacoes($idTipo,$id,$op,$textoAnedota){
	if($op == "Eliminar"){
		$url = "http://62.28.132.141/phprestsql/Anedota/" . $id;
		$session = curl_init($url);
		curl_setopt($session, CURLOPT_CUSTOMREQUEST , "DELETE");
		curl_setopt($session, CURLOPT_HEADER, true); 
    	curl_setopt($session, CURLOPT_RETURNTRANSFER, false);
		$resposta = curl_exec ($session);
		curl_close ($session);
		header("Location:http://localhost/cliente.php"); 
								
	}else if($op == "Editar"){
		if ($_GET['Edita'] == "Enviar") { 
			$url = "http://62.28.132.141/phprestsql/Anedota/" . $id;
			$texto = utf8_encode($_GET['TextoAnedota']);
			$param="Texto=" . $texto;
			$session = curl_init($url);
			curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
			curl_setopt($session, CURLOPT_POSTFIELDS,$param);
			curl_setopt($session, CURLOPT_HEADER, true); 
    		curl_setopt($session, CURLOPT_RETURNTRANSFER, false);
			$resposta = curl_exec ($session);
			curl_close ($session);
			header("Location:http://localhost/cliente.php");
			 		
		}else{
			echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";
			echo "<div id=\"EditaAnedota\">";
				echo "<form name=\"editaEliminaForm\" action=\"" . htmlentities($_SERVER['PHP_SELF']) . "\" method=\"GET\" >";
					echo "<input type=\"hidden\" name=\"TipoAnedota\" value=\"" . $idTipo . "\">";
					echo "<input type=\"hidden\" name=\"Anedota\" value=\"" . $id . "\">";
					echo "<input type=\"hidden\" name=\"Operacao\" value=\"Editar\" />";
					echo "<label for=\"novaAnedota\">Edita Anedota:" . $id . "</label>";
				echo "<div>";	
					echo "<textarea class=\"textarea\" name=\"TextoAnedota\" cols=\"20\" rows=\"20\">" . $textoAnedota. "</textarea>";
					echo "<input type=\"submit\" name=\"Edita\" value=\"Enviar\" />";
				echo "</div>";
				echo "</form></div>";
		}	
		
	}else if($op == "Nova"){
		 if($_GET['Nova'] == "Enviar"){
			$url = "http://62.28.132.141/phprestsql/Anedota";
			$texto = utf8_encode($_GET['novaAnedota']);
			$param= "Texto=" . $texto . "\n"  . "idTipoAnedota=" . urlencode($idTipo) ;
			$session = curl_init($url);
			curl_setopt($session, CURLOPT_POST, true);
			curl_setopt($session, CURLOPT_POSTFIELDS,$param);
			curl_setopt($session, CURLOPT_HEADER, true); 
    		curl_setopt($session, CURLOPT_RETURNTRANSFER, false);
			$resposta = curl_exec ($session);
			curl_close ($session);
			header("Location:http://localhost/cliente.php"); 
			
		}else{
			echo "<div id=\"NovaAnedota\">";
			echo "<form name=\"editaEliminaForm\" action=\"" . htmlentities($_SERVER['PHP_SELF']) . "\" method=\"GET\" >";
				echo "<input type=\"hidden\" name=\"TipoAnedota\" value=\"" . $idTipo . "\">";
				echo "<input type=\"hidden\" name=\"TextoAnedota\" value=\"" . $textoAnedota . "\">";
				echo "<input type=\"hidden\" name=\"Operacao\" value=\"Nova\" />";
				echo "<label for=\"novaAnedota\">Nova Anedota:</label>";
			echo "<div>";	
				echo "<textarea class=\"textarea\" name=\"novaAnedota\" cols=\"20\" rows=\"20\">insira aqui a sua nova anedota</textarea>";
				echo "<input type=\"submit\" name=\"Nova\" value=\"Enviar\" />";
			echo "</div>";
			echo "</form></div>";	
			
		}
	}
	
	
}

?>
<html>
<body>
<div id="Anedotas">
	<?php
	if ($_GET['Operacao']){
		efectuaOperacoes($_GET['TipoAnedota'],$_GET['Anedota'],$_GET['Operacao'], $_GET['TextoAnedota']);		
	} else if(!$_GET['TipoAnedota']){ ?>
	<form action="<?php htmlentities($_SERVER['PHP_SELF']) ?>"  method="GET">
		<table>
		<tr><td>
			<label for="AnedotaDoDia">Anedota Do Dia</label>
			<input type="checkbox" class="checkbox" name="AnedotaDoDia" value="1" />
		</td></tr>
		<?php
			foreach($tiposAnedotas as $key => $value){
				echo "<tr><td>";
				$chave=$key. "-" .$value;
				echo "<input type=\"submit\" name=\"TipoAnedota\" value=\"$chave\" />";
				echo "</td></tr>";
			}
		}else{
				mostraAnedotas($_GET['TipoAnedota'],$_GET['AnedotaDoDia']);	
			}
		?>
		</table>
	</form>
 </div>
</body>
</html>
